package cs342.uwosh.edu.QuizApp;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import cs342.uwosh.edu.R;
import cs342.uwosh.edu.QuizApp.database.Encryption;
import cs342.uwosh.edu.QuizApp.thread.AsyncMethod;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;
import android.widget.ListAdapter;
import android.widget.ListView;

public class ScoreHistoryActivity extends Activity {
	String[] scores = new String[] {"", "", "", "", "", "", "", "", ""};
			String user;
	ArrayAdapter<String> adapter;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scores);
       ListView listView = (ListView)findViewById(R.id.listView1);
       Intent me = this.getIntent();
       Bundle me2=  me.getExtras();
       user = me2.getString("z");

       


        String url = "http://cs346.cs.uwosh.edu/students/klappt98/scoresDownload.php?user="+ user;
	 	 AsyncMethod<ScoreHistoryActivity> downloadScores = new AsyncMethod<ScoreHistoryActivity>(ScoreHistoryActivity.class, this, "readTextFromURL");
	 	downloadScores.execute(url);

	

        
	 	downloadScores.getResult();

        listView.setAdapter(adapter);
       // listView.add
    }
	
	
    @SuppressWarnings("unchecked")
	public boolean readTextFromURL(String urlS)
    {
    	Encryption enc = new Encryption();
    	
    	
    	URL url;
        HttpURLConnection connection = null;  
        try 
        {
          //Create connection
        	System.out.println("LOLTESTBEFRE");
            url = new URL(urlS);
            connection = (HttpURLConnection)url.openConnection();


          //Get Response from PHP, just a text message, not really required.  
          InputStream is = connection.getInputStream();
          BufferedReader rd = new BufferedReader(new InputStreamReader(is));
          String line;


          String[] splitLine;
          int i = 0;
          while((line = rd.readLine()) != null) {
        	  if(line.equals(""))
        	  {
        		  break;
        		  
        	  }
        	  System.out.println(line);

              splitLine = line.split(" \\| ");
              //
        	 // System.out.println(splitLine[0] + "AAAAAA"+ splitLine[1] + "BBBB" + splitLine[2]);
              String decrypted = enc.downloadScore("http://cs346.cs.uwosh.edu/students/gustrd63/Project3/scoreDownload.php?username="+user+ "&quizname=" +splitLine[0] );
        	 System.out.println("http://cs346.cs.uwosh.edu/students/gustrd63/Project3/scoreDownload.php?username="+user+ "&quizname=" +splitLine[0]);
        	 System.out.println(decrypted);
              String title = "Quiz: " +splitLine[0] + "    Score: "+ decrypted +  " of " +splitLine[2];
        	 scores[i] = title;
        			  i++;
          }
          rd.close();

        } 
        catch (Exception e) 
        {   

          e.printStackTrace();
        } 
        finally 
        {   
          if(connection != null) 
            connection.disconnect(); 
        }
    	
        adapter = new ArrayAdapter<String>(this,
       		android.R.layout.simple_list_item_1, android.R.id.text1, scores);

        return true;
    }
}
